package com.ruoyi.web.controller.product;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.product.domain.ProductCart;
import com.ruoyi.product.service.IProductCartService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 购物车Controller
 *
 * @author tanfp
 * @date 2021-01-02
 */
@Controller
@RequestMapping("/product/productCart")
public class ProductCartController extends BaseController {
    private String prefix = "product/productCart";

    @Autowired
    private IProductCartService productCartService;

    @RequiresPermissions("product:productCart:view")
    @GetMapping()
    public String productCart() {
        return prefix + "/productCart";
    }

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("product:productCart:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductCart productCart) {
        startPage();
        List<ProductCart> list = productCartService.selectProductCartList(productCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @RequiresPermissions("product:productCart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductCart productCart) {
        List<ProductCart> list = productCartService.selectProductCartList(productCart);
        ExcelUtil<ProductCart> util = new ExcelUtil<ProductCart>(ProductCart.class);
        return util.exportExcel(list, "productCart");
    }

    /**
     * 新增购物车
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存购物车
     */
    @RequiresPermissions("product:productCart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductCart productCart) {
        return toAjax(productCartService.insertProductCart(productCart));
    }

    /**
     * 修改购物车
     */
    @GetMapping("/edit/{keyId}")
    public String edit(@PathVariable("keyId") Long keyId, ModelMap mmap) {
        ProductCart productCart = productCartService.selectProductCartById(keyId);
        mmap.put("productCart", productCart);
        return prefix + "/edit";
    }

    /**
     * 修改保存购物车
     */
    @RequiresPermissions("product:productCart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductCart productCart) {
        return toAjax(productCartService.updateProductCart(productCart));
    }

    /**
     * 删除购物车
     */
    @RequiresPermissions("product:productCart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productCartService.deleteProductCartByIds(ids));
    }
}
